package cn.siriusbot.siriuspro.admin.webapi;


import cn.siriusbot.siriuspro.admin.service.ServerConfigService;
import cn.siriusbot.siriuspro.config.aop.PowerInterceptor;
import cn.siriusbot.siriuspro.error.MsgException;
import cn.siriusbot.siriuspro.web.R.R;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.time.Duration;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;

@Log4j2
@RestController
@RequestMapping("/api/config")
public class ConfigController {


    /**
     * HttpClient单例
     */
    public static final OkHttpClient httpClient = new OkHttpClient.Builder()
            .callTimeout(Duration.ofSeconds(10))     // 设置超时时间为10秒
            .build();

    String configName = "system.config";
    Set<String> configNameSet = new ConcurrentSkipListSet<>();

    @Autowired
    ServerConfigService serverConfigService;

    @PostConstruct
    public void init(){
        configNameSet.add("hook.url");
        configNameSet.add("hook.privateKey");
    }


    /**
     * 设置配置
     */
    @RequestMapping("/set")
    @PowerInterceptor(power = 0)
    public R getEscape(
            String name,
            String val
    ){
        if (!configNameSet.contains(name)){
            throw new MsgException(500, "错误的配置项名");
        }
        serverConfigService.addConfig(configName + "." + name, val);
        return new R()
                .setCode(0)
                .setMsg("设置成功");
    }

    /**
     * 设置配置
     */
    @RequestMapping("/get")
    @PowerInterceptor(power = 0)
    public R getEscape(
            String name
    ){
        if (!configNameSet.contains(name)){
            throw new MsgException(500, "错误的配置项名");
        }
        String string = serverConfigService.getString(configName + "." + name);
        return new R()
                .setCode(0)
                .setData(string);
    }

    @RequestMapping("/ui-config")
    public R getSource(
    ){
        Request build = new Request.Builder()
                .url("http://res.siriusbot.cn/info.json")
                .build();
        try {
            Response response = httpClient.newCall(build).execute();
            if (response.code() == 200) {
                String string = Objects.requireNonNull(response.body()).string();
                return new R()
                        .setCode(0)
                        .setData(JSONObject.parseObject(string));
            }
            throw new MsgException(500, String.format("httpClient请求错误代码:%d，body:%s", response.code(), response.body().string()));
        } catch (MsgException e) {
            log.error("httpClient请求错误", e);
            throw e;
        } catch (Exception e) {
            log.error("httpClient请求错误", e);
            throw new MsgException(500, "httpClient请求错误!");
        }
    }

}
